package com.health.controller;

import com.health.pojo.ComboForm;
import com.health.service.ComboAddService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ComboAddServlet", value = "/ComboAddServlet")
public class ComboAddServlet extends HttpServlet {

    ComboAddService comboAddService = new ComboAddService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ComboForm comboForm;
        HttpSession session = request.getSession();

        int servicetype = 1;//获取服务类型
        String type = request.getParameter("serviceType");
        if (type != null) {
            servicetype = Integer.parseInt(type);
        }

        String examine_id = "0"; //获取套餐id
        String id = request.getParameter("id");
        if (id != null) {
            examine_id = id;
        }


        comboForm = (ComboForm) session.getAttribute("comboForm");

        if (comboForm == null) {
            comboForm = new ComboForm();

        }
        comboAddService.dispath(servicetype, comboForm, examine_id);
        session.setAttribute("comboForm", comboForm);

        // 设置aside侧边栏状态
        request.setAttribute("asideStatus", 2);
        request.setAttribute("openInputView", -1);
        // 转发视图
        request.getRequestDispatcher("/restrictedPages/admin.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String examine_name = request.getParameter("name");
        String examine_price = request.getParameter("price");

        HttpSession session = request.getSession();
        ComboForm comboForm = (ComboForm) session.getAttribute("comboForm");
        comboForm.setExamineCombo(examine_name);
        comboForm.setExaminePrice(examine_price);

        comboAddService.addComboform(comboForm);

        session.setAttribute("comboForm", comboForm);

        response.setContentType("text/html; charset=utf-8");
        if (comboForm.getExamineId() != 0) {
            response.getWriter().print("添加成功");
            response.setHeader("refresh", "3;ComboAddServlet");
        }


    }
}
